1054D - Changing Array - CodeForces Solution


greedy implementation *1900

Please click on ads to support us..

Python Code:

n,k,*a=map(int,open(0).read().split());m={0:1};p=A=0
for a in a:p^=a;x,p=min((m.get(k,0),k)for k in[2**k-1^p,p]);A+=x;m[p]=x+1
print(n*-~n//2-A)

C++ Code:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <unordered_map>
#include <climits>
#include <unordered_set>

using namespace std;

typedef long long LL;

class CF1054D {
public:
    LL solve(const vector<int> &a, int k) {
        LL n = a.size();
        unordered_map<int, int> xor2cnt;
        int mask = (1 << k) - 1;

        xor2cnt[0]++;
        for (int i = 0, xorSum = 0; i < n; i++) {
            xorSum ^= a[i];
            int newXorSum = min(xorSum, xorSum ^ mask);
            xor2cnt[newXorSum]++;
        }
        LL ans = (1 + n) * n / 2;
        for (auto &ite : xor2cnt) {
            ans -= calcZero(ite.second);
        }
        return ans;
    }
    LL calcZero(LL cnt) {
        LL x = cnt / 2;
        LL y = cnt - x;
        return x * (x - 1) / 2 + y * (y - 1) / 2;
    }
};

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    printf("%lld\n", CF1054D().solve(a, k));
    return 0;
}


Comments

Submit
0 Comments
More Questions

165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom